package com.hdjk.view.callback;

import com.example.liangmutian.mypicker.DateUtil;
import com.hdjk.view.entity.Performance;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

public abstract class GetPerformanceChartCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject obj = (JSONObject) msg.getData();
        if (obj!=null) {
            List<Performance> list = new ArrayList<>();
            if (obj.length() == 0) {
                msg.setData(list);
                return msg;
            }

            String key = obj.keys().next();
            JSONArray array = (JSONArray) obj.getJSONObject(key).getJSONArray("performanceChartDetails");
            if (null == array || array.length() == 0) return msg;

            for (int j = 0; j < array.length(); j++) {
                Performance performance = new Performance();
                JSONObject object = array.getJSONObject(j);
                if (!object.isNull("performance")) {
                    performance.setPerformance(object.getDouble("performance"));
                }
                if (!object.isNull("navDate")) {
                    performance.setNavDate(DateUtil.formatDate(object.getLong("navDate"), "yyyy-MM-dd"));
                }
                list.add(performance);
            }

            msg.setData(list);
        }
        return msg;
    }
}